package com.bleeth.fx.view.ssh;

import com.bleeth.fx.component.MButton;
import com.bleeth.fx.helper.DialogHelper;
import com.bleeth.fx.helper.SStyleHelper;
import com.bleeth.fx.helper.ThemeHelper;
import com.bleeth.fx.helper.TreeEntity;
import com.bleeth.fx.layout.BaseBuilder;
import com.bleeth.fx.layout.GirdBuilder;
import com.bleeth.fx.layout.RowBuilder;
import javafx.application.Application;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * @author ：Bleeth
 * @date ：2022-03-15
 * @description：
 */
public class RightView extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        final SplitPane root = new SplitPane();
        root.setDividerPositions(0.3);

        GirdBuilder leftPaneGBer = GirdBuilder.create()
                .addVBuilder(BaseBuilder.create().setId("row-tree").setSpan(1))
                .addRow("row-tree", BaseBuilder.create().setSpan(1).setPos(Pos.CENTER).debug(true));

        GirdBuilder rightPaneGBer = GirdBuilder.create()
                .addVBuilder(
                        BaseBuilder.create().setId("row-title").setHeight(50D),
                        BaseBuilder.create().setId("empty").setHeight(20D),
                        BaseBuilder.create().setId("row-username").setHeight(30D).setSpacing(20D),
                        BaseBuilder.create().setHeight(40D),
                        BaseBuilder.create().setId("row-password").setHeight(30D).setSpacing(20D),
                        BaseBuilder.create().setHeight(40D),
                        BaseBuilder.create().setId("row-login").setHeight(40D).setSpacing(20D)
                )
                .addRow("row-title", BaseBuilder.create().setSpan(1).setPos(Pos.CENTER))
                .addCell(0, "title", new Label("欢迎大家的到来"))

                .addRow("row-username", BaseBuilder.create().setSpan(1), BaseBuilder.create().setWidth(80D).setPos(Pos.CENTER_RIGHT),
                        BaseBuilder.create().setWidth(300D).setPos(Pos.CENTER_LEFT), BaseBuilder.create().setSpan(1))
                .addCell(1, "label-username", new Label("用户名"))
                .coverCell(2, "input-username", new TextField(""))

                .addRow("row-password", BaseBuilder.create().setSpan(1), BaseBuilder.create().setWidth(80D).setPos(Pos.CENTER_RIGHT),
                        BaseBuilder.create().setWidth(300D).setPos(Pos.CENTER_LEFT), BaseBuilder.create().setSpan(1))
                .addCell(1, "label-password", new Label("密  码"))
                .coverCell(2, "input-password", new TextField(""))

                .addRow("row-login", BaseBuilder.create().setSpan(1).setPos(Pos.CENTER), BaseBuilder.create().setWidth(150D).setPos(Pos.CENTER), BaseBuilder.create().setSpan(1).setPos(Pos.CENTER))
                .coverCell(1, "btn-login", MButton.builder().type(ThemeHelper.PRIMARY).text("登  录").build().make());

        treeBuild(leftPaneGBer, primaryStage);

        VBox leftPane = leftPaneGBer.build();
        VBox rightPane = rightPaneGBer.build();
        root.getItems().addAll(leftPane, rightPane);
        leftPane.prefWidthProperty().bind(root.prefWidthProperty().multiply(root.getDividers().get(0).positionProperty()));
        rightPane.prefWidthProperty().bind(root.prefWidthProperty()
                .multiply(new SimpleDoubleProperty(1D).subtract(root.getDividers().get(0).positionProperty())));

        leftPane.prefHeightProperty().bind(root.prefHeightProperty());
        rightPane.prefHeightProperty().bind(root.prefHeightProperty());

        Scene scene = new Scene(root, 1200D, 800D);
        root.prefHeightProperty().bind(scene.heightProperty());
        root.prefWidthProperty().bind(scene.widthProperty());
        primaryStage.setResizable(false);
        primaryStage.setScene(scene);
        primaryStage.show();
    }


    public void treeBuild(GirdBuilder gBer, Stage primaryStage) {
        TreeView<TreeEntity> treeView = new TreeView();
        treeView.setRoot(new TreeItem());
        treeView.setShowRoot(false);

        RowBuilder rowBuilder = gBer.getRowBuilder("row-tree");
        rowBuilder.coverCell(0, treeView);

        ContextMenu menu = new ContextMenu();
        MenuItem dirMenuItem = new MenuItem("新建目录");
        MenuItem fileMenuItem = new MenuItem("新建文件");

        menu.getItems().add(dirMenuItem);
        menu.getItems().add(fileMenuItem);

        treeView.setOnMouseClicked(event -> {
                menu.show(treeView, event.getScreenX(), event.getScreenY());
        });


        treeView.getSelectionModel().selectedItemProperty().addListener(
                new ChangeListener<TreeItem<TreeEntity>>() {
                    @Override
                    public void changed(ObservableValue<? extends TreeItem<TreeEntity>> observableValue, TreeItem<TreeEntity> oldItem, TreeItem<TreeEntity> newItem) {
                        System.out.println(newItem.getValue());
                    }
                });


        dirMenuItem.setOnAction(event -> {
            //dialog
            GirdBuilder dialogBuilder = GirdBuilder.create().addVBuilder(BaseBuilder.create().setSpan(1).debug(false), BaseBuilder.create().setSpan(1).debug(false))
                    .addRow(0, BaseBuilder.create().setSpan(1).setPos(Pos.CENTER).setSpacing(50D))
                    .addCell(0, new Label("文件家名称"))
                    .addCell(0, "input-dir", new TextField(""))

                    .addRow(1, BaseBuilder.create().setSpan(1).setPos(Pos.CENTER).setSpacing(50D))
                    .addCell(0, "btn-ok", MButton.builder().text("确认").type(ThemeHelper.SUCCESS).build().make())
                    .addCell(0, "btn-cancel", MButton.builder().text("取消").type(ThemeHelper.DANGER).build().make());

            VBox box = dialogBuilder.build();
            Stage dialog = DialogHelper.builder().title("设置文件夹名称").width(450D).height(150D).parentStage(primaryStage).content(box).build().make();

            Button btnOk = dialogBuilder.getRegion("btn-ok", Button.class);
            Button btnCancel = dialogBuilder.getRegion("btn-cancel", Button.class);
            btnOk.setOnAction(btnEvent -> {
                TreeItem<TreeEntity> selectedItem = treeView.getSelectionModel().getSelectedItem();
                if(selectedItem==null){
                    TextField textField = dialogBuilder.getRegion("input-dir", TextField.class);
                    treeView.getRoot().getChildren().add(new TreeItem<TreeEntity>(TreeEntity.create(textField.getText())));
                    dialog.close();
                }else{
                    TextField textField = dialogBuilder.getRegion("input-dir", TextField.class);
                    selectedItem.getChildren().add(new TreeItem<>(TreeEntity.create(textField.getText())));
                }

            });

            btnCancel.setOnAction(btnEvent -> {
                dialog.close();
            });

            dialog.showAndWait();
        });


    }


    public void setStyle(GirdBuilder gBer) {
        VBox root = gBer.build();


        Region titleRegion = gBer.getRegion("title");
        Region labelUsernameRegion = gBer.getRegion("label-username");
        Region inputUsernameRegion = gBer.getRegion("input-username");

        Region labelPasswordRegion = gBer.getRegion("label-password");
        Region inputPasswordRegion = gBer.getRegion("input-password");
        Region btnPasswordRegion = gBer.getRegion("btn-login");

        SStyleHelper.node(titleRegion)
                .addStyle("-fx-font-size", "18")
                .apply();

        SStyleHelper.node(labelUsernameRegion)
                .addStyle("-fx-font-size", "18")
                .apply();

        SStyleHelper.node(labelPasswordRegion)
                .addStyle("-fx-font-size", "18")
                .apply();


    }

    public void setAction(GirdBuilder gBer) {
        Region btnLogin = gBer.getRegion("btn-login");
        btnLogin.setOnMouseClicked(event -> {
            System.err.println("login...");
        });

    }


    public static void main(String[] args) {
        launch(args);
    }

}
